package com.wsoft.hr.pm.controller.sys;

import com.wsoft.hr.pm.entity.sys.HrOrganizationEntity;
import com.wsoft.hr.pm.query.sys.*;
import com.wsoft.hr.pm.service.sys.IHrOrganizationService;
import com.wsoft.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 *  前端控制器
 * @author zhanggukai
 * @since 2025年02月17日 15:27:01
 */
@RestController
@Tag(name = "人力-绩效-组织管理")
@RequestMapping("/hr/pm/org")
public class HrOrganizationController {

    @Resource
    private IHrOrganizationService organizationService;

    @Operation(summary = "初始化组织", description = "初始化组织")
    @PostMapping("/initOrg")
    public ResponseResult initOrg(@RequestBody @Valid OrgInitQuery query) {
       organizationService.initPmOrganization(query);
       return ResponseResult.success("[sys.msg.success]");
    }


    @Operation(summary = "新增组织", description = "新增组织")
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody @Valid HrOrgInsertQuery query) {
        boolean b = organizationService.insert(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "修改组织", description = "修改组织")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Valid HrOrgUpdateQuery query) {
        boolean b = organizationService.update(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "删除组织", description = "删除组织")
    @GetMapping("/delete")
    public ResponseResult delete(@RequestParam("id") Long id) {
        boolean b = organizationService.delete(id);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }
    @Operation(summary = "获取组织详情", description = "获取组织详情")
    @GetMapping("/get")
    public ResponseResult getOrg(@RequestParam("id") Long id) {
        HrOrganizationEntity entity = organizationService.getOrgById(id);
        return ResponseResult.success(entity);
    }
    @Operation(summary = "获取一级组织组织列表", description = "获取一级组织组织列表")
    @PostMapping("/listFirstOrg")
    public ResponseResult list(@RequestBody @Valid HrOrgListQuery query) {
        return ResponseResult.success(organizationService.getOrgList(query));
    }

    @Operation(summary = "获取树形组织列表", description = "获取树形组织列表")
    @PostMapping("/listAllDeptTree")
    public ResponseResult listAllDeptTree() {
        return ResponseResult.success(organizationService.selectOrgListTree());
    }

    @Operation(summary = "批量获取组织数据",description = "批量获取组织数据")
    @PostMapping("/getByIds")
    public ResponseResult getByIds(@RequestBody @Valid HrOrgGetQuery query) {
        List<HrOrganizationEntity> list = organizationService.listByIds(query.getIds());
        return ResponseResult.success(list);
    }
}
